public class Solution {
    public int StrToInt(String str) {
        
        //首先判空
        if(str.equals("") || str.equals(null)){
            return 0;
        }
        char[] array = str.toCharArray();
        int i = 0;
        while(array[i] == ' '){
            i++;
        }
        
        //判断是否有正负号
        boolean flag = true;
        if(array[i] == '-'){
            flag = false;
            i++;
        }else if(array[i] == '+'){
            i++;
        }
        
        int num = 0;
        //遍历字符数组求和
        while(i < array.length){
            if(array[i] < '0' || array[i] >'9'){
                return 0;
            }else{
                num = (flag == true) ? num*10+array[i]-'0' : num*10-(array[i]-'0');
                if(num > Integer.MAX_VALUE || num < Integer.MIN_VALUE){
                    return 0;
                }
            }
            i++;
        }
        return num;
    }
}